package cn.didi.feidi.javabackend.server.waterdeliver.repository;

import cn.didi.feidi.javabackend.server.waterdeliver.constant.address.MysqlJpaHttpAddress;
import cn.didi.feidi.javabackend.server.waterdeliver.constant.address.RedisHttpAddress;
import org.springframework.web.client.RestTemplate;

import java.io.Serializable;
import java.lang.reflect.ParameterizedType;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static cn.didi.feidi.javabackend.server.waterdeliver.utils.UrlUtil.getServerURL;

/**
 * Created by lindanpeng on 2017/7/18.
 */
public class BaseRep<T> {
    /*请求路径前缀*/
    protected String pathPrefix;
    /*泛型类型*/
    protected Class<T> clazz;

    RestTemplate restTemplate = new RestTemplate();
    public BaseRep(){
        ParameterizedType type = (ParameterizedType) this.getClass().getGenericSuperclass();//获得直接超类
        this.clazz = (Class) type.getActualTypeArguments()[0];//获得超类的第一个泛型参数的类型
        this.pathPrefix="/"+this.clazz.getSimpleName();
    }
    public T getById(Serializable id,boolean needCache){
        String methodName=Thread.currentThread().getStackTrace()[1].getMethodName();
        Map<String, Object> params = new HashMap<>();
        String path=pathPrefix+"/"+methodName;
        params.put("id", id);
        T entity=null;
        if(true==needCache)
        {
             entity = restTemplate.getForObject(getServerURL(RedisHttpAddress.redisHost, RedisHttpAddress.redisPort, path, params),clazz);
            if(entity!=null)
                return entity;
        }
         return restTemplate.getForObject(getServerURL(MysqlJpaHttpAddress.mysqlJpaHost, MysqlJpaHttpAddress.mysqlJpaPort, path, params),clazz);
    }
    public void deleteById(Serializable id,boolean needCache){
        String methodName=Thread.currentThread().getStackTrace()[1].getMethodName();
        Map<String, Object> params = new HashMap<>();
        String path=pathPrefix+"/"+methodName;
        params.put("id", id);
        T entity=null;
        if(true==needCache) {
            restTemplate.delete(getServerURL(RedisHttpAddress.redisHost,  RedisHttpAddress.redisPort, path, params));
        }
        restTemplate.delete(getServerURL(MysqlJpaHttpAddress.mysqlJpaHost,MysqlJpaHttpAddress.mysqlJpaPort, path, params));
    }
    public void update(T entity,boolean needCache){
        String methodName=Thread.currentThread().getStackTrace()[1].getMethodName();
        String path=pathPrefix+"/"+methodName;
        if(true==needCache)
        {
           restTemplate.postForObject(getServerURL(RedisHttpAddress.redisHost, RedisHttpAddress.redisPort, path, null),entity,String.class);
        }
        restTemplate.postForObject(getServerURL(MysqlJpaHttpAddress.mysqlJpaHost, MysqlJpaHttpAddress.mysqlJpaPort, path, null),entity,String.class);

    }
    public void add(T entity,boolean needCache){
        String methodName=Thread.currentThread().getStackTrace()[1].getMethodName();
        String path=pathPrefix+"/"+methodName;
        if(true==needCache)
        {
            restTemplate.postForObject(getServerURL(RedisHttpAddress.redisHost, RedisHttpAddress.redisPort, path, null),entity,String.class);
        }
        restTemplate.postForObject(getServerURL(MysqlJpaHttpAddress.mysqlJpaHost, MysqlJpaHttpAddress.mysqlJpaPort, path, null),entity,String.class);
    }
    public T getUniqueByConditions(){
        return null;
    }
    public List<T> getListByConditions(){
        return null;
    }
    public Long countByConditions(){
        return null;
    }

}
